# coding=utf-8
from django.shortcuts import render_to_response
from django.core.exceptions import ObjectDoesNotExist
from apps.research.models import ResearchGroup, ResearchArticle, ResearchPublication

def head_group():
    return list(ResearchGroup.objects.all())

def research(request, lang, department, article_id):
#   语言检验
    if lang != 'en' and lang != 'zh':
        return render_to_response('404.html', {'error': 'Unkown language error'})
#   检验部门
    groups = list(ResearchGroup.objects.all())
    if department is None:
        return render_to_response('research.html', {
                'current_item': 'research',
                'lang': lang,
                'url_en': '/en/research/',
                'url_zh': '/zh/research/',
                'head_group_list': head_group(),
                'group_list': groups})
    try:
        department = ResearchGroup.objects.get(group_tag=department)
    except ObjectDoesNotExist:
        return render_to_response('404.html', {'error': 'Unkown department error'})
    except:
        return render_to_response('404.html')
#   过滤Draft和部门
    articles = list(ResearchArticle.objects.filter(article_status=False).filter(article_group=department))  #   article_group_id or

#   参数为空，将article_id置为最近的一篇id
    if article_id is None:
        if lang == 'en' and department.group_home_en != '':
            return render_to_response('research/research_intro.html', {
                    'current_item': 'research',
                    'lang': lang, 
                    'url_en': '/en/research/'+department.group_tag,
                    'url_zh': '/zh/research/'+department.group_tag,
                    'head_group_list': head_group(),
                    'current_department': department.group_tag,
                    'group_intro': department.group_home_en,
                    'article_list': articles,
                    'group_list': groups})
        if lang == 'zh' and department.group_home_zh != '':
            return render_to_response('research/research_intro.html', {
                    'current_item': 'research',
                    'lang': lang, 
                    'url_en': '/en/research/'+department.group_tag,
                    'url_zh': '/zh/research/'+department.group_tag,
                    'head_group_list': head_group(),
                    'current_department': department.group_tag,
                    'group_intro': department.group_home_zh,
                    'article_list': articles,
                    'group_list': groups})
        try:
            article_id = articles[0].id
        except IndexError:      #   articles为空，返回
            return render_to_response('research/research_detail.html', {
                    'current_item': 'research',
                    'lang': lang, 
                    'url_en': '/en/research/'+department.group_tag,
                    'url_zh': '/zh/research/'+department.group_tag,
                    'head_group_list': head_group(),
                    'current_department': department.group_tag,
                    'group_list': groups})
        except:
            return render_to_response('404.html')   # 未知错误
#   参数不合法时返回404页面
    try:
        article_id = int(article_id)
    except KeyError:
        return render_to_response('404.html', {'error': 'Invalid article_id'}) 
    except:
        return render_to_response('404.html')

    try:
        article = ResearchArticle.objects.get(id=article_id)
    except ObjectDoesNotExist:
        return render_to_response('404.html', {'error': 'Page not found'})
    except:
        return render_to_response('404.html')

    return render_to_response('research/research_detail.html', {
            'current_item': 'research',
            'lang': lang, 
            'url_en': '/en/research/'+department.group_tag+'/'+str(article_id),
            'url_zh': '/zh/research/'+department.group_tag+'/'+str(article_id),
            'head_group_list': head_group(),
            'group_list': groups,
            'article_list': articles,
            'current_department': department.group_tag,
            'selected_article': article})

def publication(request, lang):
    if lang != 'en' and lang != 'zh':
        return render_to_response('404.html', {'error': 'Unkown language error'})
    publications = list(ResearchPublication.objects.all())
    groups = list(ResearchGroup.objects.all())
    return render_to_response('research/publications.html', {
            'current_item': 'research',
            'lang': lang, 
            'url_en': '/en/research/publications/',
            'url_zh': '/zh/research/publications/',
            'head_group_list': head_group(),
            'group_list': groups,
            'publication_list': publications})
